package com.iflytek.ssm.service.impl;

import com.iflytek.ssm.dao.UserDao;
import com.iflytek.ssm.entity.User;
import com.iflytek.ssm.service.UserService;
import com.iflytek.ssm.utils.RegexUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLException;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserDao userDao;

    @Override
    public Integer fun() {
        System.out.println("UserServiceImpl.fun");
        return 0;
    }

    /**
     * 修改密码
     *
     * @param id
     * @param oldPwd
     * @param newPwd
     * @return 0：成功，-1：用户不存在，1:旧密码不正确。。。
     * @throws SQLException
     */
    @Override
    public Integer modifyPwd(Integer id, String oldPwd, String newPwd) throws SQLException {

        // 校验旧密码是否正确
        User user = userDao.getById(id);
        if (null == user) {
            System.out.println("用户不存在，id=" + id);
            return -1;
        }
        if (!oldPwd.equals(user.getPwd())) {
            System.out.println("旧密码不正确");
            return 1;
        }

        // 校验新密码是否满足规则(正则表达式)
        boolean success = RegexUtil.cheackPwd(newPwd);
        if (!success) {
            System.out.println("新密码不满足规则要求，请重新输入");
            return 2;
        }

        //调用业务层修改密码
        Integer num = userDao.updatePwdById(newPwd, id);
        if (1 == num) {
            //修改成功
            return 0;
        } else {
            return -1;
        }

    }

}
